package com.example.mytest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import org.apache.http.Header;


public class MainActivity extends AppCompatActivity {

    Context context = this;
    Activity activity = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);

        TextView tv = findViewById(R.id.tv);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(activity, "ma2", Toast.LENGTH_SHORT).show();

                Intent intent = new Intent();
                intent.setClass(context, Main3Activity.class);
                context.startActivity(intent);
            }
        });




        WebView webView = findViewById(R.id.wv);
        webView.setWebChromeClient(new WebChromeClient());    //app外显示——跳转到第三方浏览器显示
//        webView.setWebViewClient(new MyBrowser());  //app内显示
//        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
//        webView.setWebViewClient(new WebViewClient() {
//            public boolean shouldOverrideUrlLoading(WebView view, String url)
//            { //  重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
//                view.loadUrl(url);
//                return true;
//            }
//        });

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
//        webSettings.setUseWideViewPort(true);
//        webSettings.setSupportZoom(true);
//        webSettings.setLoadsImagesAutomatically(true);
//
//        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
//        //localStorage  允许存储
//        webSettings.setDomStorageEnabled(true);
//        webSettings.setAppCacheMaxSize(1024*1024*8);//存储的最大容量
//        String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
//        webSettings.setAppCachePath(appCachePath);
//        webSettings.setAllowFileAccess(true);
//        webSettings.setAppCacheEnabled(true);
//        webSettings.setJavaScriptEnabled(true);
//        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);


//        webSettings.setCacheMode(0);
//        // 设置可以访问文件
//        webSettings.setDomStorageEnabled(true);
//        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
//            webSettings.setAllowFileAccessFromFileURLs(true);
//        }
//        webSettings.setAllowFileAccess(true);
//        webSettings.setAllowFileAccessFromFileURLs(true);
//        webSettings.setAllowContentAccess(true);
//        webSettings.setAllowFileAccessFromFileURLs(true);
//        webSettings.setCacheMode(0);
//
//        webView.loadDataWithBaseURL(getAssets(), s, "text/html", "utf-8", null);


//        webSettings.setLoadWithOverviewMode(true);
//        webSettings.setUseWideViewPort(true);
//        webSettings.setSupportMultipleWindows(true);
//        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
//        webView.setHorizontalScrollBarEnabled(false);
//        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
//        webSettings.setAllowFileAccessFromFileURLs(true);
//        webSettings.setAllowUniversalAccessFromFileURLs(true);

        //js与android调用
//        webView.loadUrl("file:///android_asset/html5-bluetooth-demo/index.html");
//        webView.loadUrl("file:///android_asset/web/index.jsp");
//        webView.loadUrl("http://192.168.50.212:8080/web2/getServerList?telephone=1");
        webView.loadUrl("http://192.168.50.212:8080/web2");
//        webView.loadUrl("http://192.168.0.104:8080/web2/getServerList?telephone=1&flag=1");
//        webView.loadUrl("file:///android_asset/MyH5_vue/index.html");
//        webView.loadUrl("file:///android_asset/web/index2.html");
//        webView.loadUrl("file:///android_asset/web2/pages/index/index.vue");

//        home();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_scrolling, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        Log.e("onOptionsItemSelected=", "==="+id);

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Log.e("onOptionsItemSelected=1", "==="+id);
            Toast.makeText(this, "setting", Toast.LENGTH_SHORT).show();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private class MyBrowser extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    public void show(View view){
        Log.e("show===", "==="+view);
//        Toast.makeText(context, "bug", Toast.LENGTH_SHORT).show();
//        Toast.makeText(context,"请选择开始于截止日期",Toast.LENGTH_SHORT).show();

//        new Handler(Looper.getMainLooper()).post(new Runnable() {
//            @Override
//            public void run() {
//                Toast.makeText(activity, "进入消息中心", Toast.LENGTH_SHORT).show();
//            }
//        });
    }

    private void home() {
        Log.e("home===", "===" + BaseApplication.rid);

        RequestParams params = new RequestParams();
//		params.add("deviceId", BaseApplication.rid);

//		http://192.168.0.105:8083/
//		http://192.168.0.104:8080/web2/getServerList?telephone=1&flag=1
        HttpHelper.get(context, "http://192.168.0.104:8080/web2/getServerList?telephone=1&flag=1", new TextHttpResponseHandler() {
            @Override
            public void onStart() {
//                m_myHandler.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        if (loadingDialog != null && !loadingDialog.isShowing()) {
//                            loadingDialog.setTitle("请稍等");
//                            loadingDialog.show();
//                        }
//                    }
//                });


            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//                m_myHandler.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        Log.e("home_f", throwable.toString() + "===" + responseString);
//
//                        if (loadingDialog != null && loadingDialog.isShowing()) {
//                            loadingDialog.dismiss();
//                        }
//                        UIHelper.ToastError(context, throwable.toString());
//
//                        finish();
//                    }
//                });


            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, final String responseString) {
                new Handler().post(new Runnable() {
                    @Override
                    public void run() {
                        try {

//                                ResultConsel result = JSON.parseObject(responseString, ResultConsel.class);

                            Log.e("home===1", "===" + responseString);

                            Toast.makeText(context, responseString, Toast.LENGTH_SHORT).show();

//							if("".equals(responseString)){
////								IntentUtils.startActivity(activity, DeviceLogonActivity.class); //跳转7寸屏注册界面
//								UIHelper.goToAct(context, DeviceLogonActivity.class);
//							}else{
//								UserBean bean = JSON.parseObject(responseString, UserBean.class);
//
////                                IntentUtils.startActivity(activity, MainActivity.class);
//								SharedPreferencesUrls.getInstance().putString("device_id", BaseApplication.rid);
//								SharedPreferencesUrls.getInstance().putInt("id", bean.getId());
//
//								Intent intent = new Intent();
//								intent.setClass(context, MainActivity.class);
//								intent.putExtra("device_id", BaseApplication.rid);
//								startActivity(intent);
//							}
//
//							finish();

                        } catch (Exception e) {
                            Log.e("getDeviceByDeviceId===e", "===" + e);
                            e.printStackTrace();
                        }

//                        if (loadingDialog != null && loadingDialog.isShowing()) {
//                            loadingDialog.dismiss();
//                        }
                    }
                });

            }


        });

    }
}